package prepare.huawei;


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * description: <a href="https://blog.nowcoder.net/n/d1ecc1863dc240a2ab19ad079431519e"> 最优任务调度 </a>
 *
 * @author <a href="zhaoyingling12@163.com">simon zhao</a>
 * @since 2023/3/23
 */
public class OptimalTaskScheduling {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String tasks = input.nextLine();
        String[] taskArray = tasks.split(",");
        int n = input.nextInt();
        // key 任务类型 value 该任务出现的次数
        Map<String, Integer> taskCntMap = new HashMap<>();
        int maxTaskCnt = 0;
        for (String t : taskArray) {
            int cnt = taskCntMap.getOrDefault(t, 0);
            cnt++;
            maxTaskCnt = Math.max(maxTaskCnt, cnt);
            taskCntMap.put(t, cnt);
        }
        int extraTaskCnt = 0;
        for (String t : taskCntMap.keySet()) {
            if (taskCntMap.get(t) == maxTaskCnt) {
                extraTaskCnt++;
            }
        }
        int consumeTime = Math.max((maxTaskCnt - 1) * (n + 1) + extraTaskCnt, taskArray.length);
        System.out.println(consumeTime);
    }
}
